package com.zs.games.utils;

import com.zs.games.resultTool.CimResponse;
import com.zs.games.resultTool.ResultCodeEnum;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import javax.annotation.Nullable;
import java.io.Serializable;
import java.util.Optional;

/**
 * @title: 返回信息
 * @author: LiChao
 * @dateTime: 2020/7/1 12:08
 * @description:
 **/
@Data
@ApiModel(description = "返回信息")
public class CimResult<T> implements Serializable {

    public CimResult(){}

    private static final long serialVersionUID = 1L;
    @ApiModelProperty(value = "状态码", required = true)
    private long code;
    @ApiModelProperty(value = "是否成功", required = true)
    private boolean success;
    @ApiModelProperty("承载数据")
    private T data;
    @ApiModelProperty(value = "返回消息", required = true)
    private String msg;

    private CimResult(CimResponse resultCode) {
        this(resultCode, null, resultCode.getMessage());
    }

    private CimResult(CimResponse resultCode, String msg) {
        this(resultCode, null, msg);
    }

    private CimResult(CimResponse resultCode, T data, String msg) {
        this(resultCode.getCode(), data, msg);
    }

    private CimResult(long code, T data, String msg) {
        this.code = code;
        this.data = data;
        this.msg = msg;
        this.success = ResultCodeEnum.SUCCESS.getCode() == code;
    }

    public static boolean isSuccess(@Nullable CimResult<?> result) {
        return Optional.ofNullable(result).map((x) -> ObjectUtil.nullSafeEquals(ResultCodeEnum.SUCCESS.getCode(), x.getCode())).orElse(Boolean.FALSE);
    }

    public static boolean isNotSuccess(@Nullable CimResult<?> result) {
        return !isSuccess(result);
    }

    public static <T> CimResult<T> data(T data) {
        return data(data, "操作成功");
    }

    public static <T> CimResult<T> data(T data, String msg) {
        return data(ResultCodeEnum.SUCCESS.getCode(), data, msg);
    }

    public static <T> CimResult<T> data(long code, T data, String msg) {
        return new CimResult(code, data, data == null ? "暂无承载数据" : msg);
    }

    public static <T> CimResult<T> success(String msg) {
        return new CimResult(ResultCodeEnum.SUCCESS.getCode(),null, msg);
    }

    public static <T> CimResult<T> success(CimResponse resultCode) {
        return new CimResult(resultCode);
    }

    public static <T> CimResult<T> success(CimResponse resultCode, String msg) {
        return new CimResult(resultCode, msg);
    }

    public static <T> CimResult<T> fail(String msg) {
        return new CimResult(ResultCodeEnum.FAIL.getCode(),null, msg);
    }

    public static <T> CimResult<T> fail(long code, String msg) {
        return new CimResult(code, null, msg);
    }

    public static <T> CimResult<T> fail(CimResponse resultCode) {
        return new CimResult(resultCode);
    }

    public static <T> CimResult<T> fail(CimResponse resultCode, String msg) {
        return new CimResult(resultCode, msg);
    }

    public static <T> CimResult<T> status(boolean flag) {
        return flag ? success("操作成功") : fail("操作失败");
    }

    @Override
    public String toString() {
        return "CimResult(code=" + this.getCode() + ", success=" + this.isSuccess() + ", data=" + this.getData() + ", msg=" + this.getMsg() + ")";
    }
}
